package com.cetc.sdp.kmga.cs.common;

import org.apache.hadoop.hbase.util.Bytes;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @description:
 * @author： DengQiang
 * @date: 2017/8/24 13:46
 */
public class TableMapping implements Serializable {

    private int id;

    private String tableName;

    private int rowkeyType;

    private String topic;

    private String queue;

    private String logType;

    private Map<String, Column> columnMap;

    private Column rowKey;

    private int generateCreateTime;

    /**
     * 是否分表
     */
    private boolean split;

    /**
     * 分表表名
     */
    private Map<String, String> splitTables;

    /**
     * 分表索引表
     */
    private Map<String, List<IndexTable>> splitIndexTables;

    /**
     * 表名自动添加后缀
     */
    private int autoAppendSuffix;

    private boolean repartition;

    private int numOfPartition;

    private long initial;

    private long current;

    private LocalDateTime lastUpdateTime;

    /**
     * 索引表
     */
    private List<IndexTable> indexTables;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public int getRowkeyType() {
        return rowkeyType;
    }

    public void setRowkeyType(int rowkeyType) {
        this.rowkeyType = rowkeyType;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getQueue() {
        return queue;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }

    public String getLogType() {
        return logType;
    }

    public void setLogType(String logType) {
        this.logType = logType;
    }

    public void addColumn(Column column) {
        if (column == null) {
            return;
        }
        if (columnMap == null) {
            this.columnMap = new HashMap<>();
        }
        if (column.getIsRowKey() == 1) {
            this.rowKey = column;
        }
        this.columnMap.put(column.getColumnName(), column);
    }

    public Column getColumn(String columnName) {
        return columnMap.get(columnName);
    }

    public int getGenerateCreateTime() {
        return generateCreateTime;
    }

    public void setGenerateCreateTime(int generateCreateTime) {
        this.generateCreateTime = generateCreateTime;
    }

    public int getAutoAppendSuffix() {
        return autoAppendSuffix;
    }

    public void setAutoAppendSuffix(int autoAppendSuffix) {
        this.autoAppendSuffix = autoAppendSuffix;
    }

    public Column getRowKey() {
        return rowKey;
    }

    public Map<String, Column> getColumnMap() {
        return columnMap;
    }

    public long getInitial() {
        return initial;
    }

    public void setInitial(long initial) {
        this.initial = initial;
    }

    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public boolean isRepartition() {
        return repartition;
    }

    public void setRepartition(boolean repartition) {
        this.repartition = repartition;
    }

    public int getNumOfPartition() {
        return numOfPartition;
    }

    public void setNumOfPartition(int numOfPartition) {
        this.numOfPartition = numOfPartition;
    }

    public LocalDateTime getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(LocalDateTime lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public List<IndexTable> getIndexTables() {
        return indexTables;
    }

    public void setIndexTables(List<IndexTable> indexTables) {
        this.indexTables = indexTables;
    }

    public boolean isSplit() {
        return split;
    }

    public void setSplit(boolean split) {
        this.split = split;
    }

    public Map<String, String> getSplitTables() {
        return splitTables;
    }

    public void setSplitTables(Map<String, String> splitTables) {
        this.splitTables = splitTables;
    }

    public Map<String, List<IndexTable>> getSplitIndexTables() {
        return splitIndexTables;
    }

    public void setSplitIndexTables(Map<String, List<IndexTable>> splitIndexTables) {
        this.splitIndexTables = splitIndexTables;
    }

    public static class Column implements Serializable {

        private String columnName;

        private int innerIdex;

        private String columnFamily;

        private int mappingIndex;

        private byte[] columnNameBytes;

        private byte[] familyBytes;

        private int isRowKey;

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
            if (columnName != null) {
                this.columnNameBytes = Bytes.toBytes(columnName);
            }
        }

        public String getColumnFamily() {
            return columnFamily;
        }

        public void setColumnFamily(String columnFamily) {
            this.columnFamily = columnFamily;
            if (columnFamily != null) {
                this.familyBytes = Bytes.toBytes(columnFamily);
            }
        }

        public int getMappingIndex() {
            return mappingIndex;
        }

        public void setMappingIndex(int mappingIndex) {
            this.mappingIndex = mappingIndex;
        }


        public int getIsRowKey() {
            return isRowKey;
        }

        public void setIsRowKey(int isRowKey) {
            this.isRowKey = isRowKey;
        }

        public byte[] getColumnNameBytes() {
            return columnNameBytes;
        }

        public byte[] getFamilyBytes() {
            return familyBytes;
        }

        public int getInnerIdex() {
            return innerIdex;
        }

        public void setInnerIdex(int innerIdex) {
            this.innerIdex = innerIdex;
        }
    }
}
